//
//  JSPicView.m
//  JSBasicFramework
//
//  Created by Apple on 2018/8/29.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSPicView.h"

@implementation JSPicView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self didInitialize];
    }
    return self;
}
-(void)didInitialize{
    NSMutableArray *picViews = [NSMutableArray new];
    for (int i = 0; i < 9; i++) {
        YYAnimatedImageView *imageView = [YYAnimatedImageView new];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.size = CGSizeMake(100, 100);
        imageView.hidden = YES;
        imageView.clipsToBounds = YES;
        imageView.exclusiveTouch = YES;
        [self addSubview:imageView];
        [picViews addObject:imageView];
    };
    _pics = picViews.copy;
    _padding = UIEdgeInsetsMake(5, 5, 5, 5);
    _itemMargin = 5;
}

- (void)setDataArray:(NSArray<id<JSPicViewProtocol>> *)dataArray{
    
    __weak __typeof(self)weakSelf = self;

    
    [self settingData:dataArray customChildLayout:^CGRect(JSPicView *picView, UIView *childView, NSInteger idex) {
            CGFloat w = 0;
            CGFloat h = 0;
            CGPoint origin = {0};
            if (dataArray.count == 1) {
                
                origin.x = weakSelf.padding.left;
                origin.y = weakSelf.padding.top;
                w = 200;
                h = 200;
            }else if (dataArray.count < 5){
                w = 120;
                h = w;
                origin.x = (idex % 2) ? (w + self.padding.left + self.itemMargin) : (self.padding.left);
                //            origin.y = (idex / 2) * (w + kRealValue(10));
                origin.y = (idex / 2) ? (w + self.padding.top + self.itemMargin) : (self.padding.top);
            }else{
                w = h = 80;
                //            origin.x = (idex % 3) * (w + kRealValue(10));
                origin.x = (idex % 3) ? (self.padding.left + (idex % 3) * (w + self.itemMargin)):(self.padding.left);
                //            origin.y = (idex / 3) * (w + kRealValue(10));
                origin.y = (idex / 3) ? (self.padding.top + (idex / 3) * (w + self.itemMargin)):(self.padding.top);
            }
        return CGRectMake(origin.x, origin.y, w, h);

    }];
}
-(void)settingData:(NSArray <id<JSPicViewProtocol>> *)dataArray customChildLayout:(CGRect(^)(JSPicView *picView, UIView *childView, NSInteger idex))customLayout{
    NSAssert(customLayout, @"不能为空");
    _dataArray = dataArray;
    CGRect upRect = CGRectMake(self.padding.left, self.padding.top, 0, 0);
    NSInteger repeatW = 0;
    NSInteger repeatH = 0;
    for (UIImageView *item in _pics) {
        [item cancelCurrentImageRequest];
    }
    for (NSInteger i = 0; i < 9; i++) {
        UIImageView *imageView = self.pics[i];
        if (i >= dataArray.count) {
            [imageView cancelCurrentImageRequest];
            imageView.hidden = YES;
        } else {
            imageView.hidden = NO;
            id<JSPicViewProtocol> obj = dataArray[i];
            CGRect curRect = customLayout(self,imageView,i);
//            [imageView setImageWithURL:[NSURL URLWithString:[obj imagePath]] options:YYWebImageOptionSetImageWithFadeAnimation];
            [imageView setImageWithURL:[NSURL URLWithString:[obj imagePath]] placeholder:UIImageMake(@"noData")];
            dispatch_async(dispatch_get_main_queue(), ^{
                imageView.frame = curRect;
            });
            //MARK:计算容器大小
            if (upRect.origin.y == curRect.origin.y) { // 累计宽度
                upRect = CGRectMake(upRect.origin.x, upRect.origin.y, upRect.size.width + curRect.size.width, upRect.size.height);
                repeatW ++; // 累计次数 用于累计间距
            }
            if (upRect.origin.x == curRect.origin.x) { // 累计高度
                upRect = CGRectMake(upRect.origin.x, upRect.origin.y, upRect.size.width, upRect.size.height + curRect.size.width);
                repeatH++;
            }
            
        }
        
    }
    CGFloat wrapW = upRect.origin.x + upRect.size.width + (repeatW - 1) * self.itemMargin + self.padding.right;
    CGFloat wrapH = upRect.origin.y + upRect.size.height + (repeatH - 1) * self.itemMargin + self.padding.bottom;
    self.wrapSize = CGSizeMake(wrapW, wrapH);
    
}
- (CGSize)sizeThatFits:(CGSize)size{

    return self.wrapSize;
    
}

@end
